package miniBanco.Mapper;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import miniBanco.Dto.Log;
import miniBanco.Exceptions.HandledSQLException;
import miniBanco.Mapper.Interface.ILog;
import basics.DBManager;

public class LogDAO implements ILog 
{
	@Override
	public ArrayList<Log> getAllLog()    
	{	
		Log retorna = null;
		ArrayList<Log> erroresList = new ArrayList<Log>();
		String sql = "SELECT * FROM Log";
		Connection c = DBManager.connect();
		try {
			Statement s = c.createStatement();
			ResultSet rs = s.executeQuery(sql);			
			
			while(rs.next()) {
				String u = rs.getString("Tag");
				String e = rs.getString("Mensaje");

				retorna = new Log();
				retorna.setMensaje(e);
				retorna.setTag(u);
				erroresList.add(retorna);
			}
			
		} catch (SQLException ex) {
				DBManager.Error(ex);
				try {
					c.rollback();
				} catch (SQLException e) {
					DBManager.Error(e);
					throw new HandledSQLException("Se produjo un error al hacer el rollback :(.");
				}
				throw new HandledSQLException(ex.getMessage());

		} finally {
				try {
					c.close();
				} catch (SQLException e) {
					DBManager.Error(e);
					throw new HandledSQLException("Se produjo un error al cerrar la conexion.");
				}
		}
		return erroresList;
	}

	

}
